package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.SimplePanel;
import ge.telasi.tasks.ui.flowpermission.TaskStatusCombo;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * @author dimitri
 */
public class TaskCommentPane extends SimplePanel implements ActionEditor {
    private Task task;
    private JTextArea txtComment;
    private TaskStatusCombo cmbStatus;

    public TaskCommentPane() {
        super();
        setPreferredSize(new Dimension(100, 200));
        setBorder(BorderFactory.createEmptyBorder(5, 0, 0, 0));
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
        doServerCommunication();
        cmbStatus.setSelection(task.getTaskStatus());
    }

    public void clear() {
        txtComment.setText("");
        if (task != null) {
            cmbStatus.setSelection(task.getTaskStatus());
        }
    }

    public boolean canClose() {
        return true;
    }

    public boolean save() {
        if (Platform.getDefault().validateConnection()) {
            try {
                Platform pl = Platform.getDefault();
                Map credentials = pl.getCredentials();
                String comment = txtComment.getText();
                int status = cmbStatus.getSelection();
                task = pl.getFacade().changeTaskStatus(credentials, task, comment, status);
                return true;
            } catch(Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        }
        return false;
    }

    public void focusOnStart() {
        txtComment.requestFocus();
    }

    @Override
    protected JComponent createMainComponent() {
        JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
        // combo
        JPanel jPanel1 = new JPanel(new BorderLayout(5, 5));
        JLabel lbl = new JLabel("მიუთითეთ დავალების სტატუსი:");
        lbl.setHorizontalAlignment(JLabel.RIGHT);
        cmbStatus = new TaskStatusCombo();
        jPanel1.add(lbl, BorderLayout.CENTER);
        jPanel1.add(cmbStatus, BorderLayout.EAST);
        mainPanel.add(jPanel1, BorderLayout.NORTH);
        // editor
        txtComment = new JTextArea();
        txtComment.setBorder(null);
        txtComment.setWrapStyleWord(true);
        txtComment.setLineWrap(true);
        mainPanel.add(new JScrollPane(txtComment));
        // return main panel
        return mainPanel;
    }

    @Override
    protected boolean serverCommunicationProcess() throws Exception {
        cmbStatus.setAllowedStatuses(task);
        return cmbStatus.getItemCount() > 0;
    }

    @Override
    protected void serverCommunicationComplete() {
    }

}
